import requests
import json
from jsonpath import jsonpath
import time
from openpyxl import workbook

wb = workbook.Workbook()  # 创建excel对象
ws = wb.active  # 激活表对象
# 创建表头
ws.append(['名称', '价格', '涨跌额', '市值', '交易额', '流动性', '交易次数', '持币地址数', '持币地址'])

url1 = "https://api.msgsender.org/api/getPairs"

payload = '{\"chainType\":3,\"checkTime\":0,\"dexType\":1,\"pageId\":1,\"pageSize\":200,\"sort\":0,\"type\":2}'
headers = {
  'Accept': 'application/json, text/plain, */*',
  'Accept-Language': 'zh,zh-CN;q=0.9,en;q=0.8',
  'Connection': 'keep-alive',
  'Content-Type': 'application/json;charset=UTF-8;',
  'Origin': 'https://tools.msgsender.io',
  'Referer': 'https://tools.msgsender.io/',
  'Sec-Fetch-Dest': 'empty',
  'Sec-Fetch-Mode': 'cors',
  'Sec-Fetch-Site': 'cross-site',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
  'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '"Windows"'
}

response = requests.request("POST", url1, headers=headers, data=payload)

# print(response.text)
r = response.text
p = json.loads(r)
# print(type(p))
pairAddress = jsonpath(p,"$..pairAddress")
world = jsonpath(p, "$..token0")
# print(pairAddress)
# # print(len(world))
# print(type(world))

for i in pairAddress:
  url = "https://api.msgsender.org/api/get24DayLine"
  # print(i)
  payload = '{"pairAddress":"%s","startTime":0,"chainType":3,"type":4}' % i
  headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh,zh-CN;q=0.9,en;q=0.8',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json;charset=UTF-8;',
    'Origin': 'https://tools.msgsender.io',
    'Referer': 'https://tools.msgsender.io/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"'
  }

  response = requests.request("POST", url, headers=headers, data=payload)

  html_text = response.text
  html_p = json.loads(html_text)
  name1 = jsonpath(p, "$..token0Symbol")
  name2 = jsonpath(p, "$..token1Symbol")
  price = jsonpath(html_p,"$..close")
  increase = jsonpath(html_p,"$..increase")
  shizhi = jsonpath(html_p,"$..price")
  jiaoyie = jsonpath(html_p,"$..goldPrice")
  liudongxing = jsonpath(html_p, "$..totalLiquidity")
  jiaoyicishu = jsonpath(html_p, "$..volume")
  chibidizhishu = jsonpath(html_p, "$..holders")
  chibidizhi = jsonpath(p, "$..token0")
  # for a, b, c, d, e, f, g, h, i, j in zip(name1,name2,price,increase,shizhi,jiaoyie,liudongxing,jiaoyicishu,chibidizhishu,chibidizhi):
  #   name = a + '\\' + b
  #   my_list = [name, c, d, e, f, g, h, i, j]
  #   ws.append(my_list)
  #   wb.save("股票详情.xlsx")
  #   wb.close()
